<template>
  <BasicTable :rowSelection="rowSelection" :formActions="formActions" @rowClick="rowClick"
              :handle-search-info-fn="handleSearchInfoFn" @register="registerTable">
    <template #action="{record}">
      <a-popconfirm title="确定删除吗" @confirm="() => remove(record)">
        <a-button type="link" @click.stop="() => {}">删除</a-button>
      </a-popconfirm>
    </template>
  </BasicTable>
  <UnitModal ref="refUnitModal" @ok="reload" />
</template>
<script setup>
import BasicTable from "@/components/Table/src/BasicTable.vue";
import { useListPage } from "@/hooks/system/useListPage";
import { defHttp } from "@/utils/http/axios";
import { DeleteOutlined, PlusOutlined } from "@ant-design/icons-vue";
import { real } from "@/utils";
import UnitModal from "@/views/cwqs/UnitModal.vue";
import { shallowRef } from "vue";

const refUnitModal = shallowRef();
defineOptions({ name: "Unit" });
const Api = {
  getAllUnit: "/cwqs/cwqsUnit/list"
};
const getAllUnit = (params) => defHttp.get({ url: Api.getAllUnit, params }, { joinParamsToUrl: true });
const { tableContext } = useListPage({
  designScope: "data-source-list",
  tableProps: {
    title: "财务账簿列表",
    api: getAllUnit,
    columns: [
      {
        dataIndex: "unitCode",
        title: "单位编码"
      },
      {
        dataIndex: "unitName",
        title: "单位名称"
      },
      {
        dataIndex: "unitFullName",
        title: "单位全称"
      },
      {
        dataIndex: "accountingCode",
        title: "账簿编码"
      },
      {
        dataIndex: "accountingKey",
        title: "账簿主键"
      },
      {
        dataIndex: "division",
        title: "行政区划",
      }
    ],
    formConfig: {
      schemas: [
        {
          label: "单位编码",
          field: "unitCode",
          component: "Input",
          colProps: { span: 6 }
        },
        {
          label: "单位名称",
          field: "unitName",
          component: "Input",
          colProps: { span: 6 }
        },
        {
          label: "单位全称",
          field: "unitFullName",
          component: "Input",
          colProps: { span: 6 }
        },
        {
          label: "账簿编码",
          field: "accountingCode",
          component: "Input",
          colProps: { span: 6 }
        },
        {
          label: "账簿主键",
          field: "accountingKey",
          component: "Input",
          colProps: { span: 6 }
        },
      ]
    },
    rowSelection: {
      getCheckboxProps() {
        return {
          onClick(e) {
            e.stopPropagation();
          }
        }
      }
    }
  },
});
const [registerTable, { reload, updateTableDataRecord }, {
  rowSelection,
  selectedRows,
  selectedRowKeys
}] = tableContext;
const formActions = () => {
  return [
    {
      type: "primary",
      title: "新建",
      icon: PlusOutlined,
      onClick: () => {
        refUnitModal.value.setData();
      }
    },
    {
      danger: true,
      title: "批量删除",
      icon: DeleteOutlined,
      disabled: !real(selectedRowKeys).length,
      confirmTitle: '确定删除吗',
      confirmInfo: '删除后不可恢复, 确定删除吗',
      confirm: deleteBatch
    }
  ];
};

function remove(record) {
  defHttp.delete({ url: "/cwqs/cwqsUnit/delete", params: { id: record.id } }, { joinParamsToUrl: true }).then(reload);
}

function deleteBatch() {
  defHttp.delete({
    url: "/cwqs/cwqsUnit/deleteBatch",
    params: { ids: real(selectedRowKeys).toString() }
  }, { joinParamsToUrl: true }).then(reload);
  selectedRowKeys.value = [];
}

function selectChange(...args) {
  console.log('selectChange', args)
}
function rowClick(record) {
  refUnitModal.value.setData(record.id)
}
function handleSearchInfoFn(params) {
  if (params) {
    for (const key in params) {
      if (params[key]) {
        params[key] = `*${params[key]}*`;
      }
    }
  }
}
</script>
